package org.example.game.history;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/06/02 10:39
 */
public class Solution100311 {
    public static void main(String[] args) {

        System.out.println(1&2&4&5);

        Solution100311 solution100311 = new Solution100311();
        System.out.println(solution100311.countDays(57, new int[][]{{3,49},{23,44},{21,56},{26,55},{23,52},{2,9},{1,48},{3,31}}));
    }


    //1 days 从1到n
    //2 meetings 处理为 数组i + days  按照id排序处理
    public int countDays(int days, int[][] meetings) {

        HashMap<Integer, Integer> startDays = new HashMap<>();

        for (int i = 0; i < meetings.length; i++) {
            Integer len = meetings[i][1] - meetings[i][0];
            startDays.putIfAbsent(meetings[i][0], len);
            if(startDays.get(meetings[i][0]) < len){
                startDays.put(meetings[i][0], len);
            }
        }

        Object[] nums = startDays.keySet().toArray();
        Arrays.sort(nums);

        int count = 0;
        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            int num = Integer.parseInt(nums[i].toString());
            if(startDays.get(num) != null){
                int len = startDays.get(num);
                if(num>index){
                    count += len + 1;
                }else if(num+len > index){
                    count += num+len-index;
                }
                index = Math.max(index, num + len);
            }
        }

        return  days - count;
    }

}
